//
//  JCLEmoticonTipView.swift
//  WeiBo
//
//  Created by DDC on 2017/9/30.
//  Copyright © 2017年 DDC. All rights reserved.
//

import UIKit
import pop
class JCLEmoticonTipView: UIImageView {

    // 记录第一次按钮的模型
    fileprivate var preEmoticon:JCLEmoticon?
    // 设置模型属性
    var emoticon:JCLEmoticon? {
        didSet {
 
            // 跳出循环
            if emoticon == preEmoticon {
                return
            }
            // 记录当前的表情模型
            preEmoticon = emoticon
            
            tipButton.setTitle(emoticon?.emojiStr, for: [])
            tipButton.setImage(emoticon?.image, for: [])
            
            // 设置动画
            let anim:POPSpringAnimation = POPSpringAnimation(propertyNamed: kPOPLayerPositionY)
            anim.fromValue = 40
            anim.toValue = 24
            anim.springBounciness = 20
            anim.springSpeed = 20
            tipButton.layer.pop_add(anim, forKey: nil)
        }
    }
    fileprivate lazy var tipButton = UIButton()
    // 构造函数
    init() {
        
        let image = UIImage(named: "emoticon_keyboard_magnifier")
        super.init(image: image)
        
        // 设置锚点
        layer.anchorPoint = CGPoint(x: 0.5, y: 1.2)
        
        // 设置表情按钮 
        addSubview(tipButton)
        
        tipButton.frame = CGRect(x: 0, y: 8, width: 36, height: 36)
        tipButton.center.x = bounds.width * 0.5
        tipButton.titleLabel?.font = UIFont.systemFont(ofSize: 32)
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
